<?php

session_start();

require( 'util.php' );

if (!can_post_new_page())
{
	report_err_and_exit( '您没有在本版发帖的权限。' );
}

validate_action_get( array( 'mid', 'mode' ) );
$mid = $_GET['mid'];
$mode = $_GET['mode'];

if (strcasecmp( $mode, 'post' ) == 0 )
{
	validate_action_post( array( 'title', 'msg' ) );

	require_once( 'db.php' );

	$title = mysql_real_escape_string( $_POST['title'] );
	$msg = mysql_real_escape_string( $_POST['msg'] );

	$sql = "INSERT INTO page ( u_id, m_id, subject, message, dateline, last_replied ) VALUES ( {$_SESSION['u_id']}, $mid, '$title', '$msg', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP )";
	$result = mysql_query( $sql );

	if ($result)
	{
		redirect_and_exit( 'list.php?mid=' . $mid, '发帖成功' );
	}
	else
	{
		report_err_and_exit( '发帖失败(' . mysql_error() . ')' );
	}
}
else if (strcasecmp( $mode, 'write' ) == 0 )
{
	require_once( 'db.php' );
    
	$model = get_model( $mid );
    
    //********************** 页面内容开始 **********************
    
    require_once( 'page_frame.inc.php' );
    
    output_frame_header( "发表新帖子 - {$model['topic']}" );
?>


<link rel="stylesheet" type="text/css" href="static/style/forum_post.css" />
<style>
pn{vertical-align:middle;overflow:hidden;margin-right:3px;padding:0;height:23px;border:1px solid #999;background:#E5E5E5 url(static/common/pn.png) repeat-x 0 0;cursor:pointer;-moz-box-shadow:0 1px 0 #E5E5E5;-webkit-box-shadow:0 1px 0 #E5E5E5;box-shadow:0 1px 0 #E5E5E5;}
.pn:active{background-position:0 -23px;}
.ie6 .pn{overflow-x:visible;width:0;}
.pn em,.pn span,.pn strong{padding:0 10px;line-height:21px;}
.pn em,.pn strong{font-weight:700;}
.ie7 .pn em,.ie7 .pn span,.ie7 .pn strong{padding:0 5px;line-height:18px;}
a.pn{height:21px;line-height:21px;color:#444 !important;}
a.pn:hover{text-decoration:none;}
.ie6 a.pn{width:auto;}
.ie6 a.pn em,.ie6 a.pn span,.ie6 a.pn strong{display:block;}
.ie7 a.pn em,.ie7 a.pn span,.ie7 a.pn strong{line-height:21px;}
.pnc,a.pnc{border-color:#235994;background-color:#06C;background-position:0 -48px;color:#FFF !important;}
.pnc:active{background-position:0 -71px;}
.pnpost .pn{height:26px;}

px{border:1px solid;border-color:#848484 #E0E0E0 #E0E0E0 #848484;background:#FFF url(static/common/px.png) repeat-x 0 0;color:;}
.px{padding:2px 4px;line-height:17px;}
.px{height:17px;}
.px{width:auto;}
.cl:after{content:".";display:block;height:0;clear:both;visibility:hidden;}
.cl{zoom:1;}

.space14 {
clear: both;
height: 14px;
line-height: 0;
font-size: 0;
}

</style>

<script type="text/javascript" src="jquery.js"></script>
<script charset="utf-8" src="kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="kindeditor/lang/zh_CN.js"></script>
<script>

    editor = null;
    $(function() {
        editor = KindEditor.create('#msg');
		$("#title").focus();
    });
    
</script>

<!---->
<div id="postForm">
	<div class="space14"></div>
	板块:&nbsp;<a href="<?php echo 'list.php?mid=', $mid, '">', $model['topic']; ?></a><br />
	<form id="threadEditor" method="POST" action="new_page.php?mid=<?php echo $mid, '&mode=post'; ?>">
		
		<div class="space14"></div>
		<div class="pbt cl">
			<div class="z" style="vertical-align:middle">
			<span><span></span><input type="text" name="title" id="title" class="px" value="" style="width: 35em" tabindex="1" /></span>
			</div>
		</div>
		
		<div class="space14"></div>
		<textarea id="msg" name="msg" rows="15" cols="90"></textarea>
		
		<div class="space14"></div>
		<div class="mtm mbm pnpost">
			<button type="submit" id="postsubmit" class="pn pnc" value="true" name="topicsubmit">
			<span>发表帖子</span>
			</button>
		</div>
		
	</form>
	
	<div class="space14"></div>
</div>

<script>

    $("#threadEditor").submit( function()
    {
        var title = $('#title').val();
        if (title == '')
        {
            alert( '标题不能为空。' );
            $('#title').focus();
            return false;
        }
        
        var msg = editor.html();
        var empty = (!msg || msg == "");
        
        if (!empty)
        {
            editor.sync();				
            return true;
        }
        else
        {
            alert('帖子内容不能为空。');
            editor.focus();
            
            return false;
        }
    });

</script>

<?php
    //********************** 页面内容结束 **********************

    output_frame_footer();
}

?>